package svc

import (
	"github.com/silenceper/wechat/v2"
	"github.com/silenceper/wechat/v2/cache"
	"github.com/silenceper/wechat/v2/miniprogram"
	miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
	"github.com/zeromicro/go-zero/rest"
	"github.com/zeromicro/go-zero/zrpc"
	"xialou_platform/app/middleware"
	"xialou_platform/services/agent/rpc/agent"
	"xialou_platform/services/system/rpc/system"
	"xialou_platform/services/third_party/rpc/thirdPartyClient"
	"xialou_platform/services/third_party/rpc/thirdparty"
	"xialou_platform/services/wallet/api/internal/config"
	"xialou_platform/services/wallet/rpc/wallet"
)

type ServiceContext struct {
	Config        config.Config
	Auth          rest.Middleware
	AuthJwt       rest.Middleware
	WxMiniProgram *miniprogram.MiniProgram
	ThirdPartyRpc thirdPartyClient.ThirdPartyClient
	AgentRpc      agent.Agent
	WalletRpc     wallet.Wallet
	SystemRpc     system.System
}

func NewServiceContext(c config.Config) *ServiceContext {
	wc := wechat.NewWechat()
	memory := cache.NewMemory()
	cfg := &miniConfig.Config{
		AppID:     c.Wechat.Mini.AppId,
		AppSecret: c.Wechat.Mini.AppSecret,
		Cache:     memory,
	}
	miniprog := wc.GetMiniProgram(cfg)
	return &ServiceContext{
		Config:        c,
		Auth:          middleware.NewAuthMiddleware(c.Auth.AccessSecret).Handle,
		AuthJwt:       middleware.NewAuthJwtMiddleware().Handle,
		WxMiniProgram: miniprog,
		ThirdPartyRpc: thirdparty.NewThirdParty(zrpc.MustNewClient(c.ThirdPartyRpc)),
		AgentRpc:      agent.NewAgent(zrpc.MustNewClient(c.AgentRpc)),
		WalletRpc:     wallet.NewWallet(zrpc.MustNewClient(c.WalletRpc)),
		SystemRpc:     system.NewSystem(zrpc.MustNewClient(c.SystemRpc)),
	}
}
